<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>sku代换库</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">

    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="/webapp/js/common/jquery.global.settings.js"></script>

    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>


</head>
<body>
<div class="easyui-container-fluid">
    <div class="easyui-panel" style="border:none;">
        <div class="nav" id="nav"></div>
        <div class="easyui-panel" style="border:none;">
            <form id="ff">
                <div style="margin-bottom:10px;">
                    <label class="item-label">sku：</label>
                    <input type="text" id="sku" name="sku" class="easyui-textbox" data-options="prompt: '请输入sku',panelHeight:'150'" style="width:30%;"/>
                    <label class="item-label">报告范围：</label>
                    <select id="reportRadius" class="easyui-combobox" style="width: 100px;" data-options="panelHeight:'auto'">
                        <option value="1">当前</option>
                        <option value="2">全部</option>
                    </select>
                    <label class="item-label">年：</label>
                    <input type="text" id="year" name="reportYear" class="easyui-combobox" style="width: 100px;"/>
                    <span id="monthLabel">
                        <label class="item-label">月：</label>
                        <input id="month" name="reportMonth" type="text" class="easyui-combobox" style="width: 100px;"/>
                    </span>
                    <span id="weekLabel">
                        <label class="item-label">周：</label>
                        <input type="text" id="week" name="reportWeek" class="easyui-combobox" style="width: 100px;"/>
                    </span>
                    <a href="javascript:void(0);" style="margin-left:40px;width:80px;" class="easyui-linkbutton c8" onclick="searchBy()">查询</a>
                </div>
            </form>
        </div>
        <div class="table-wrapper">
            <div id="th">
                <div>
                    <a href="#" onclick="disable()" class="easyui-linkbutton" iconCls="icon-no" plain="true">禁用</a>
                    <a href="#" onclick="ok()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">启用</a>
                </div>
            </div>
            <table id="datagrid" class="easyui-datagrid" data-options="fitColumns:true,scrollbarSize:0,striped:true" style="max-height: 708px;">
                <thead>
                <tr>
                    <th field="ck" checkbox="true"></th>
                    <th data-options="field:'sku'" width="24%">sku</th>
                    <th data-options="field:'originalModel'" width="40%">标题</th>
                    <th data-options="field:'reportType',align:'center'" width="8%" formatter="reportFormatter">报告类型</th>
                    <th data-options="field:'operateUser',align:'center'" width="5%">操作人</th>
                    <th data-options="field:'updateTime',align:'center'" width="10%">操作时间</th>
                    <th data-options="field:'entityStatus',align:'center'" width="5%" formatter="statusFormatter">状态</th>
                    <th data-options="field:'action',formatter:actions,align:'center'" width="6%">操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        setNav();
        initMonthWeek("monthLabel", "weekLabel");
        $("#datagrid").datagrid({
            rownumbers: true,
            pagination:true,
            idField:"id",
            checkOnSelect:false,
            pageSize: 100,
            pageList: [100, 300, 500],
            emptyMsg:"暂无数据",
            toolbar:"#th",
            onLoadSuccess:function (data) {
                //不加这个，跳转到下一页的时候，之前的复选框不会清除掉
                $("#datagrid").datagrid("clearChecked");
            },
        }).datagrid("getPager").pagination({
            onSelectPage:searchBy
        });
    });

    function searchBy() {
        var data = {};
        //分页参数
        var pagination  = $("#datagrid").datagrid('getPager').data('pagination').options;
        data.pageNumber = pagination.pageNumber;
        data.pageSize = pagination.pageSize;
        data.param = {};
        data.param.sku = $("#sku").textbox('getValue');
        if ($("#reportRadius").combobox("getValue") == "1") {
            data.param.reportType = window.parent.workMode.toUpperCase();
            data.param.year = $("#year").combobox('getValue');
            if ($("#week").is("*")) {
                data.param.week = $("#week").combobox('getValue');
            }else {
                data.param.month = $("#month").combobox('getValue');
            }
        }

        $.ajax({
            url: config.url + "/sku/dispute-list",
            type: "post",
            data: JSON.stringify(data),
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            xhrFields: {withCredentials: true},
            beforeSend: function () {
                $.messager.progress({
                    title: "请稍等",
                    interval: 50,
                    text: "数据加载中..."
                });
            },
            success: function (data) {
                $.messager.progress("close");
                if (data.code != 200) {
                    $.messager.alert("提示",data.msg, "error");
                    return;
                }
                if (data.data) {
                    let list = data.data.list == null ? [] : data.data.list;
                    $("#datagrid").datagrid("loadData", {total:data.data.size, rows: list});
                }
            },
            error: function (data) {
                console.log(data);
                $.messager.progress("close");
            }
        });
    }

    function actions(val, row){
        var context;
        var updateStatus;
        if (row.entityStatus == 'OK') {
            context = "禁用";
            updateStatus = 2;
        } else {
            context = "启用";
            updateStatus = 1;
        }
        return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;' onclick='updateStatus(\""+row.id+"\", \"" + updateStatus+"\", \""+context+"\")'>"+context+"</a>";
    }

    function reportFormatter(val, row){
        if (row.reportType == 'WEEK') {
            return "周报[" + row.year + "-" + row.week + "]";
        }else if (row.reportType == 'MONTH') {
            return "月报[" + row.year + "-" + row.month + "]";
        }else if (row.reportType == 'ONLINE') {
            return "电商(周)[" + row.year + "-" + row.week + "]";
        }else if (row.reportType == 'ONLINE_M') {
            return "电商(月)[" + row.year + "-" + row.month + "]";
        }else if (row.reportType == 'PDK_W') {
            return "拼抖快(周)[" + row.year + "-" + row.week + "]";
        }else if (row.reportType == 'PDK_M') {
            return "拼抖快(月)[" + row.year + "-" + row.month + "]";
        }else {
            return "[" + row.reportType + "]";
        }
    }

    function updateStatus(id,entityStatus,context){
        $.messager.confirm("提示","您确定"+context+"吗？",function (f) {
            if(f){
                $.ajax({
                    url:config.url+"/sku/dispute/update-status/"+entityStatus,
                    type:"post",
                    dataType:"json",
                    data:JSON.stringify([id]),
                    contentType:"application/json;charset=UTF-8",
                    xhrFields:{withCredentials:true},
                    beforeSend:function () {
                        $.messager.progress({
                            title:"请稍等",
                            interval:50,
                            text:"操作进行中..."
                        });
                    },
                    success:function (data) {
                        $.messager.progress("close");
                        if(data.code==200 ){
                            $.messager.alert("提示","操作成功","info",function () {
                                searchBy();
                            });
                        }else {
                            $.messager.alert("提示",data.msg,"error");
                        }
                    },
                    error:function (data) {
                        $.messager.progress("close");
                        $.messager.alert("提示","系統异常","error");
                    }
                });
            }
        });
    }

    function disable(){
        var rows = $('#datagrid').datagrid('getSelections');
        if (rows.length == 0) {
            $.messager.alert("提示", "请选择禁用数据","error");
            return;
        }
        $.messager.confirm("提示", "您确定禁用这些记录吗？", function (flag) {
            if (flag) {
                var ids = [];
                for (var i = 0; i < rows.length; i++) {
                    ids.push(rows[i].id);
                }
                $.ajax({
                    url: config.url + "/sku/dispute/update-status/2",
                    type: 'post',
                    data: JSON.stringify(ids),
                    contentType: "application/json",
                    xhrFields: {withCredentials: true},
                    success: function (data) {
                        if (data.code == 200) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                searchBy();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, thrown) {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                })
            }
        });
    }
    function ok(){
        var rows = $('#datagrid').datagrid('getSelections');
        if (rows.length == 0) {
            $.messager.alert("提示", "请选择启用数据","error");
            return;
        }
        $.messager.confirm("提示", "您确定启用这些记录吗？", function (flag) {
            if (flag) {
                var ids = [];
                for (var i = 0; i < rows.length; i++) {
                    ids.push(rows[i].id);
                }
                $.ajax({
                    url: config.url + "/sku/dispute/update-status/1",
                    type: 'post',
                    data: JSON.stringify(ids),
                    contentType: "application/json",
                    xhrFields: {withCredentials: true},
                    success: function (data) {
                        if (data.code == 200) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                searchBy();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, thrown) {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                })
            }
        });
    }
</script>
</body>
</html>